using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Xceed.Chart.GraphicsCore;
using Xceed.Chart.Standard;
using Xceed.Chart;
using Xceed.Chart.Core;
namespace Gadgets.Trading
{
    public partial class eStocksInfo : WeifenLuo.WinFormsUI.Docking.DockContent 
    {
        public eStocksInfo()
        {
            InitializeComponent();
        }
        private long timestamp = 0;
        //private double last_price = 0;
        private double last_vol = 0;
        //private bool round_tick = false;
        private void eStocksInfo_Load(object sender, EventArgs e)
        {
            try
            {
                ComboBox obj = cboSymbol.ComboBox;
                ABSCommonLibrary.SetSymbols(ref obj);

                timestamp = 0;
                ABSCommonLibrary.InitChartControl(ref chartControlPrice);
                // add the line
                Chart m_Chart = chartControlPrice.Charts[0];
                LineSeries m_Line = (LineSeries)m_Chart.Series.Add(SeriesType.Line);
                m_Line.LineStyle = LineSeriesStyle.Line;
                m_Line.DataLabels.Mode = DataLabelsMode.None;
                m_Line.Legend.Mode = SeriesLegendMode.Series;
                m_Line.LineBorder.Color = Color.Indigo;
                m_Line.LineBorder.Width = 2;
                m_Line.InflateMargins = true;
                m_Line.Markers.Visible = false;
                m_Line.Name = "Price";

                // instruct the line series to use x values
                m_Line.UseXValues = true;

                m_Chart.Axis(StandardAxis.PrimaryX).ScaleMode = AxisScaleMode.Numeric;
                m_Chart.Axis(StandardAxis.PrimaryY).ScaleMode = AxisScaleMode.Numeric;
                m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.AutoMin = false;
                m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.Min = 530;
                m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.AutoMax = false;
                m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.Max = 670;
                
                ABSCommonLibrary.InitChartControl(ref chartControlVol);
                m_Chart = chartControlVol.Charts[0];

                // switch the categories axis in numeric mode
                m_Chart.Axis(StandardAxis.PrimaryX).ScaleMode = AxisScaleMode.Numeric;

                // create the shape series
                ShapeSeries m_Shape = (ShapeSeries)m_Chart.Series.Add(SeriesType.Shape);

                //// configure it
                // 1. filling is specified per data point
                m_Shape.Appearance.FillMode = AppearanceFillMode.DataPoints;

                // 2. borders are also specified per data point
                m_Shape.Appearance.LineMode = AppearanceLineMode.DataPoints;

                // 3. show information about the data points in the legend
                m_Shape.Legend.Mode = SeriesLegendMode.None;

                // 5. show the Y size and label in the data point labels
                m_Shape.DataLabels.Mode = DataLabelsMode.None;

                // use custom X positions
                m_Shape.UseXValues = true;
                m_Chart.Axis(StandardAxis.PrimaryX).ScaleMode = AxisScaleMode.Numeric;
                m_Chart.Axis(StandardAxis.PrimaryY).ScaleMode = AxisScaleMode.Numeric;
                m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.AutoMin = false;
                m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.Min = 530;
                m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.AutoMax = false;
                m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.Max = 670;
                               
                  
                //// X sizes are specified in Model units (the default is Scale)
                //// this will make the bars size independant from the scale of the X axis 
                //m_Shape.XSizesUnits = MeasurementUnits.Model;
                //// this will require to set the InflateMargins flag to true since in this mode
                //// scale is determined only by the X positions of the shape and will not take 
                //// into account the size of the bars.
                //m_Shape.InflateMargins = true;

                // position all shapes at the series Z order 
                m_Shape.UseZValues = false;

                //timerUpdate.Enabled = true;
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }

        private void eStocksInfo_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F9)
                {                    
                    chartControlPrice.Refresh();                   
                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }

        private void cboSymbol_Validated(object sender, EventArgs e)
        {                        
            cboSymbol.Text = cboSymbol.Text.ToUpper();
        }
        private void UpdateChart()
        {
            DataSet normal_exec = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC010003,
                    new object[2] { cboSymbol.Text.ToUpper(), timestamp });
            LineSeries m_Line = (LineSeries)chartControlPrice.Charts[0].Series[0];
            ShapeSeries m_Shape = (ShapeSeries)chartControlVol.Charts[0].Series[0];
            // add xy values           
            for (int i = 0; i < normal_exec.Tables[0].Rows.Count; i++)
            {
                DataRow row = normal_exec.Tables[0].Rows[i];
                m_Line.AddXY(Convert.ToDouble(row["PRICE"]),
                    Convert.ToDouble(row["TIMESTAMP"]));

                m_Shape.AddShape((Convert.ToDouble(row["ACCUMULATED_VOL"]) - last_vol) / 2, // Y center of bar -> half its Y size
                Convert.ToDouble(row["TIMESTAMP"]), // X position
                0, // Z position - not used since UseZValue is set to false
                0.2, // X size - 10 model units
                Convert.ToDouble(row["ACCUMULATED_VOL"]) - last_vol, // Y size of bar
                0, // Z size - 2 thirds of series depth
                string.Empty, // label
                new FillEffect(Color.Blue), // filling
                new LineProperties(1, Color.Blue) // border
                );
                last_vol = Convert.ToDouble(row["ACCUMULATED_VOL"]);

                if (timestamp < Convert.ToInt32(row["TIMESTAMP"]))
                    timestamp = Convert.ToInt32(row["TIMESTAMP"]);
            }

            chartControlPrice.Refresh();
            chartControlVol.Refresh();
        }
        private void timerUpdate_Tick(object sender, EventArgs e)
        {
            try
            {
                timerUpdate.Enabled = false;
                UpdateChart();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
            finally
            {
                timerUpdate.Enabled = true;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                timestamp = 0;
                last_vol = 0;
                LineSeries m_Line = (LineSeries)chartControlPrice.Charts[0].Series[0];
                m_Line.XValues.Clear();
                m_Line.ZValues.Clear();                
                m_Line.Values.Clear();
                ShapeSeries m_Shape = (ShapeSeries)chartControlVol.Charts[0].Series[0];
                m_Shape.XValues.Clear();
                m_Shape.XSizes.Clear();
                m_Shape.YSizes.Clear();
                m_Shape.Values.Clear();
                m_Shape.ZValues.Clear();
                m_Shape.ZSizes.Clear();

                Chart m_Chart = chartControlPrice.Charts[0];
                DataRow[] rows = DeclareLibrary.SYMBOLS.Tables[0]
                    .Select("CODE = '" + cboSymbol.Text.ToUpper() + "'");
                if (rows.Length > 0)
                {
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.AutoMin = false;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.Min = Convert.ToDouble(rows[0]["lower_price"]);
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.AutoMax = false;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.Max = Convert.ToDouble(rows[0]["upper_price"]);
                    //m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.MajorTickMode = MajorTickModeNumeric.Auto; 
                }

                UpdateChart();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }            
        }

        private void chartControlPrice_DoubleClick(object sender, EventArgs e)
        {
            chartControlPrice.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMax
                = !chartControlPrice.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMax;
            chartControlPrice.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMin
                = !chartControlPrice.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMin;
            chartControlPrice.Refresh();
        }
    }
}